package com.lzb.browser.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.browser.AppConfig;
import com.lzb.browser.BaseNoModelFragment;
import com.lzb.browser.Content;
import com.lzb.browser.R;
import com.lzb.browser.activity.CamouflageModeActivity;
import com.lzb.browser.activity.PriPictureActivity;
import com.lzb.browser.activity.PriVideoActivity;
import com.lzb.browser.adapter.HomeAdapter;
import com.lzb.browser.bean.HomeBean;
import com.lzb.browser.databinding.FragmentHomeBinding;
import com.lzb.browser.dialog.AddNavigationDialog;
import com.lzb.browser.utils.AppSpUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> implements OnItemClickListener {

    private List<HomeBean> listShow = new ArrayList<>();
    private HomeAdapter homeAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        mDataBinding.rvHomeList.setLayoutManager(new GridLayoutManager(mContext, 4));
        homeAdapter = new HomeAdapter();
        mDataBinding.rvHomeList.setAdapter(homeAdapter);
        homeAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (position == listShow.size() - 1) {
            showAddNavigationDialog();
        } else {
            // 处理导航项点击事件
            HomeBean item = listShow.get(position);
            String url = item.getUrl();
            if (url != null && !url.isEmpty()) {
                // 跳转到WebViewFragment并传递URL
                navigateToWebView(url);
            } else {
                if (position == 0) {
                    startActivity(PriVideoActivity.class);
                } else if (position == 1) {
                    startActivity(PriPictureActivity.class);
                } else if (position == 2) {
                    startActivity(CamouflageModeActivity.class);
                }
            }
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        List<HomeBean> listSp = AppSpUtils.getHomeList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
        } else {
            listSp = new ArrayList<>();
            listSp.add(new HomeBean(getString(R.string.video_privacy), R.drawable.shipinjiami, ""));
            listSp.add(new HomeBean(getString(R.string.picture_privacy), R.drawable.tupianjiami, ""));
            listSp.add(new HomeBean(getString(R.string.camouflage_mode), R.drawable.weizhuangmoshi, ""));
            listSp.add(new HomeBean("百度", R.drawable.baidu, "https://www.baidu.com/"));
            listSp.add(new HomeBean("头条新闻", R.drawable.toutiaoxinwen, "http://www.toutiao.com/"));
            listSp.add(new HomeBean("淘宝", R.drawable.taobao, "https://www.taobao.com/"));
            listSp.add(new HomeBean("京东", R.drawable.jingdong, "https://www.jd.com/"));
            listSp.add(new HomeBean("天气", R.drawable.tianqi, "https://m.baidu.com/s?word=%E5%A4%A9%E6%B0%94"));
            listSp.add(new HomeBean("新浪新闻", R.drawable.xinlangxinwen, "https://news.sina.com.cn/"));
            AppSpUtils.saveHomeList(listSp);
            listShow.addAll(listSp);
        }

        listShow.add(new HomeBean("", R.drawable.tianjia, ""));
        homeAdapter.setList(listShow);
    }

    /**
     * 跳转到WebViewFragment并传递URL
     *
     * @param url 要加载的URL
     */
    private void navigateToWebView(String url) {
        // 通过Activity加载URL到WebViewFragment
        if (getActivity() instanceof com.lzb.browser.HomeActivity) {
            ((com.lzb.browser.HomeActivity) getActivity()).loadUrl(url);
        }
    }

    private void showAddNavigationDialog() {
        AddNavigationDialog dialog = new AddNavigationDialog(mContext);
        dialog.setListener(new AddNavigationDialog.Ilistener() {
            @Override
            public void clickAddFolder(String title, String url) {
                List<HomeBean> listSp = AppSpUtils.getHomeList();
                listSp.add(new HomeBean(title, R.drawable.liulanqi1, url));
                AppSpUtils.saveHomeList(listSp);
                ToastUtils.showShort(getString(R.string.add_suc));
            }
        });
        dialog.show();
    }

}